let api = require('../../../api/index');
let util = require('../../../utils/util');

Page({
    image: '',
    data: {
        active: 1,
        list: [],
        form: {
            page: 1,
            pagesize: 10,
        },
        pageStatus: 1,
        countDownTimer: null
    },
    onLoad: function (options) {
        this.reload()
    },
    onShow: function () {
        if (this.data.list.length > 0) {
            this.countDownTimerOpen()
        }
    },
    onReady: function () {

    },
    onHide: function () {
        this.countDownTimerStop()
    },
    onUnload: function () {
        this.countDownTimerStop()
    },
    onPullDownRefresh: function () {
        console.log('打印日志，要不然会出现一个版本bug')

        wx.startPullDownRefresh()

        setTimeout(() => {
            this.getList('reset')

            // 停止下拉刷新
            wx.stopPullDownRefresh()
        }, 0)
    },
    onReachBottom: function () {
        this.getList()
    },
    reload() {
        console.log('初次加载')
        this.getList('reset')
        this.getImage()
    },
    getImage(){
        let that = this
        api.doctor.getActivityImage().then(res => {
            res.data.ResData.map( e => {
                if(e.type == 1){
                    that.setData({
                        image: e.image
                    })
                }
            })
        })
    },
    getList(type) {
        if (type === 'reset') {
            this.countDownTimerStop()

            this.setData({
                ['form.page']: 1,
                // list: [],
                pageStatus: 1
            })
        }


        if (this.data.pageStatus !== 1) {
            return false
        } else {
            this.setData({
                pageStatus: 5
            })
        }


        api.activity.getActivityList({
            page: this.data.form.page,
            pagesize: this.data.form.pagesize,
        }).then(res => {
            let list = res.data.ResData.Items
            let pageSize = res.data.ResData.ItemsPerPage
            let pageStatus;


            list.map((item, index) => {
                item.isBuy = false
                item.itemArray = {
                    day: "00",
                    hour: "00",
                    minute: "00",
                    second: "00"
                }
                return item
            })

            if (this.data.list.pageNum > 1) {
                list = this.data.list.concat(list);
            }

            if (list.length < pageSize) {
                if (list.length <= 0) {
                    pageStatus = 99
                } else {
                    pageStatus = 10
                }
            } else {
                pageStatus = 1
            }

            this.setData({
                list: list,
                pageStatus: pageStatus,
                ['form.page']: this.data.form.page++,
            })

            if (type === 'reset') {
                this.countDownTimerOpen()
            }
        })
    },
    countDownTimerOpen() {
        this.countDownTimerStop()

        this.countDownTimerContent()

        this.data.countDownTimer = setInterval(() => {
            // console.log('倒计时定时任务')
            this.countDownTimerContent()
        }, 1000)
    },
    countDownTimerStop() {
        if (this.data.countDownTimer) {
            clearInterval(this.data.countDownTimer)
            this.data.countDownTimer = null
        }
    },
    countDownTimerContent() {
        let time = parseInt(new Date().getTime() / 1000);
        // console.log( this.data.list,"这是个什么");
        this.data.list.forEach((item, index) => {
            let start_time = item.start_time
            let end_time = item.end_time
            let isBuy = false

            if (start_time <= time && time <= end_time) {
                isBuy = true
            }
            // console.log(end_time,time,"时间戳");
            let countDownTime = end_time - time;
            item.timeArray = util.numberToCountDown(countDownTime)
            item.isBuy = isBuy

            this.setData({
                ['list[' + index + ']']: item
            })
        })

    },
    toPagesDetails(e) {
        console.log(e)
        let info = e.currentTarget.dataset.item;

        if (info.isBuy == true) {
            wx.navigateTo({
                url: `/pages/product/detail/detail?source=zaohui&id=${info.id}`,
            })
        }
    },

})